package com.cdream.basic.classiccase.miaosha.controller;


import com.cdream.basic.mq.rocket.RocketMqTransProducer;
import com.cdream.basic.classiccase.miaosha.entity.OrderInfo;
import com.cdream.basic.classiccase.miaosha.entity.StockLog;
import com.cdream.basic.classiccase.miaosha.service.OrderInfoService;
import com.cdream.basic.classiccase.miaosha.service.StockLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zs
 * @since 2021-07-17
 */
@RestController
@RequestMapping("/miaosha/order")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @Autowired
    private StockLogService stockLogService;
    @Autowired
    private RocketMqTransProducer rocketMqTransProducer;


    @RequestMapping("/create_order_by_sql")
    public OrderInfo createOrderBySql(Integer userId, Integer itemId, Integer promoId, Integer amount) {
        return orderInfoService.createOrderBySql(userId, itemId, promoId, amount, null);
    }

    @RequestMapping("/create_order_by_redis")
    public OrderInfo createOrderByRedis(Integer userId, Integer itemId, Integer promoId, Integer amount) {
        return orderInfoService.createOrderByRedis(userId, itemId, promoId, amount, null);
    }
    @RequestMapping("/create_order_by_mq")
    public Boolean createOrderByMq(Integer userId, Integer itemId, Integer promoId, Integer amount) {
        // 可在无库存的情况下直接失败，不创建流水
        StockLog stockLog = stockLogService.initStockLog(itemId, amount);
        return rocketMqTransProducer.transAsyncReduceStock(userId, 0,itemId, amount,stockLog.getStockLogId());
    }


}
